import Mock from "mockjs";

const questions: { data: Question[] } = Mock.mock({
  "data|2100": [
    {
      id: "@id",
      "questionType|1-3": 1,
      questionTitle: "@cparagraph(1, 2)",
      questionOptionIndex: [1],
      questionAnswer: [
        { index: 1, answer: "选项1" },
        { index: 2, answer: "选项2" },
        { index: 3, answer: "选项3" },
        { index: 4, answer: "选项4" },
      ],
      remark: "@cparagraph(1, 2)",
      officialGuide: "@cparagraph(1, 2)",
      chapterId: "@id",
      knowledgeId: "@id",
      "carType|1-5": 1,
      "subject|1, 4": 1,
    },
  ],
});

// const questionActionRecords: QuestionActionRecordWithId[] = questions.data.map(
//   (question) => {
//     return {
//       questionId: question.id,
//       lastErrorAnswerRecord: null,
//       chooseAnswer: [],
//       actionCount: 2,
//       errorCount: 1,
//       updatedAt: Date.now(),
//     };
//   }
// );
const questionActionRecords: QuestionActionRecordWithId[] = [];

// cancel
export const listAllQuestionIds = async (): Promise<Res<Question["id"][]>> => {
  const res = await Mock.mock({
    code: 0,
    message: "success",
    data: questions.data.map((question) => question.id),
  });
  return res;
};

// done
export const listQuestionIdsBySetId = async (
  setId: string
): Promise<Res<Question["id"][]>> => {
  const res = await Mock.mock({
    code: 0,
    message: "success",
    data: questions.data.map((question) => question.id),
  });
  return res;
};

// done
export const listQuestionIdsByTagId = async (
  tagId: string
): Promise<Res<Question["id"][]>> => {
  const res = await Mock.mock({
    code: 0,
    message: "success",
    data: questions.data.map((question) => question.id),
  });
  return res;
};

// waiting
export const listPaperQuestionIdsBy = async (): Promise<
  Res<Question["id"][]>
> => {
  const res = await Mock.mock({
    code: 0,
    message: "success",
    data: questions.data.map((question) => question.id),
  });
  return res;
};

// done
export const listQuestionsByIds = async (
  questionIds: Question["id"][]
): Promise<Res<Question[]>> => {
  const res = await Mock.mock({
    code: 0,
    message: "success",
    data: questions.data.filter((question) =>
      questionIds.includes(question.id)
    ),
  });
  return res;
};

// 加载本用户的全部做题记录（初次打开网站时使用
export const listAllQuestionsActionRecord = async (): Promise<
  Res<QuestionActionRecordWithId[]>
> => {
  const res = await Mock.mock({
    code: 0,
    message: "success",
    data: questionActionRecords,
  });
  return res;
};

// 根据时间戳，获取该时间戳之后的全部做题记录（后续增量更新时使用
export const listQuestionsActionRecordByUpdatedAt = async (
  updatedAt: number
): Promise<Res<QuestionActionRecordWithId[]>> => {
  const res = await Mock.mock({
    code: 0,
    message: "success",
    data: questionActionRecords,
    // data: questionActionRecords.filter((item) => item.updatedAt > updatedAt),
  });
  return res;
};

// 上传做题记录（批量上传
export const updateQuestionsActionRecord = async (
  actionRecords: QuestionActionRecordWithId[]
): Promise<Res<null>> => {
  console.log("正在上传做题记录：", actionRecords);

  const res = await Mock.mock({
    code: 0,
    message: "success",
    data: null,
  });
  return res;
};

// 收藏题目
export const favoriteQuestion = async (questionIds: Question["id"][]) => {
  const res = await Mock.mock({
    code: 0,
    message: "success",
    data: true,
  });
  return res;
};

// 取消收藏题目
export const cancelFavoriteQuestion = async (questionIds: Question["id"][]) => {
  const res = await Mock.mock({
    code: 0,
    message: "success",
    data: true,
  });
  return res;
}
